package me.zhengjie.sqlservice.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import me.zhengjie.mapper.WcsLineEdgeWarehouseMapper;
import me.zhengjie.pojo.entity.WcsLineEdgeWarehouse;
import me.zhengjie.sqlservice.WcsLineEdgeWarehouseService;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

@Service
@RequiredArgsConstructor
public class WcsLineEdgeWarehouseServiceImpl extends ServiceImpl<WcsLineEdgeWarehouseMapper, WcsLineEdgeWarehouse> implements WcsLineEdgeWarehouseService {

    private final WcsLineEdgeWarehouseMapper wcsLineEdgeWarehouseMapper;

    @Override
    public List<WcsLineEdgeWarehouse> selectFloorIdleStatusContainerCodeIsNull(String floor, Integer status) {
        LambdaQueryWrapper<WcsLineEdgeWarehouse> wcsLineEdgeWarehouseLambdaQueryWrapper = new LambdaQueryWrapper<>();
        wcsLineEdgeWarehouseLambdaQueryWrapper.eq(WcsLineEdgeWarehouse::getFloor,floor)
                .eq(WcsLineEdgeWarehouse::getStatus,status)
                .eq(WcsLineEdgeWarehouse::getIsLock,0)
                .eq(WcsLineEdgeWarehouse::getCcdStatus,0)
                .isNull(WcsLineEdgeWarehouse::getContainerCode);
        return wcsLineEdgeWarehouseMapper.selectList(wcsLineEdgeWarehouseLambdaQueryWrapper);
    }

    @Override
    public boolean updateStatusIsAgv(String storageCode,String containerCode, Integer status, int isAgv) {
        LambdaUpdateWrapper<WcsLineEdgeWarehouse> wcsLineEdgeWarehouseLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        wcsLineEdgeWarehouseLambdaUpdateWrapper.set(WcsLineEdgeWarehouse::getStatus,status)
                .set(WcsLineEdgeWarehouse::getIsAgv,isAgv)
                .set(WcsLineEdgeWarehouse::getContainerCode,containerCode)
                .eq(WcsLineEdgeWarehouse::getStorageCode,storageCode);
        return wcsLineEdgeWarehouseMapper.update(null,wcsLineEdgeWarehouseLambdaUpdateWrapper) >0;
    }

    @Override
    public boolean updateStatusIsAgv(String storageCode,String containerCode, Integer status, int isAgv,int emptyTrayQuantity) {
        LambdaUpdateWrapper<WcsLineEdgeWarehouse> wcsLineEdgeWarehouseLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        wcsLineEdgeWarehouseLambdaUpdateWrapper.set(WcsLineEdgeWarehouse::getStatus,status)
                .set(WcsLineEdgeWarehouse::getIsAgv,isAgv)
                .set(WcsLineEdgeWarehouse::getContainerCode,containerCode)
                .set(WcsLineEdgeWarehouse::getEmptyTrayQuantity,emptyTrayQuantity)
                .eq(WcsLineEdgeWarehouse::getStorageCode,storageCode);
        return wcsLineEdgeWarehouseMapper.update(null,wcsLineEdgeWarehouseLambdaUpdateWrapper) >0;
    }

    @Override
    public List<WcsLineEdgeWarehouse> selectRegionStatusIsNull(String floor,String region, Integer status) {
        LambdaQueryWrapper<WcsLineEdgeWarehouse> wcsLineEdgeWarehouseLambdaQueryWrapper = new LambdaQueryWrapper<>();
        wcsLineEdgeWarehouseLambdaQueryWrapper.eq(WcsLineEdgeWarehouse::getFloor,floor)
                .eq(WcsLineEdgeWarehouse::getRegion,region)
                .eq(WcsLineEdgeWarehouse::getIsLock,0)
                .eq(WcsLineEdgeWarehouse::getStatus,status);
        return wcsLineEdgeWarehouseMapper.selectList(wcsLineEdgeWarehouseLambdaQueryWrapper);
    }

    @Override
    public WcsLineEdgeWarehouse selectStorageCode(String storageCode) {
        LambdaQueryWrapper<WcsLineEdgeWarehouse> wcsLineEdgeWarehouseLambdaQueryWrapper = new LambdaQueryWrapper<>();
        wcsLineEdgeWarehouseLambdaQueryWrapper.eq(WcsLineEdgeWarehouse::getStorageCode,storageCode)
                .eq(WcsLineEdgeWarehouse::getIsLock,0);
        return wcsLineEdgeWarehouseMapper.selectOne(wcsLineEdgeWarehouseLambdaQueryWrapper);
    }

    @Override
    public List<WcsLineEdgeWarehouse> selectCcdStatus(Integer ccdStatus) {
        LambdaQueryWrapper<WcsLineEdgeWarehouse> wcsLineEdgeWarehouseLambdaQueryWrapper = new LambdaQueryWrapper<>();
        wcsLineEdgeWarehouseLambdaQueryWrapper.eq(WcsLineEdgeWarehouse::getCcdStatus,ccdStatus)
                .eq(WcsLineEdgeWarehouse::getIsLock,0);
        return wcsLineEdgeWarehouseMapper.selectList(wcsLineEdgeWarehouseLambdaQueryWrapper);
    }

    @Override
    public boolean updateManualFeeding(Integer id, String containerCode, int status) {
        LambdaUpdateWrapper<WcsLineEdgeWarehouse> wcsLineEdgeWarehouseLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        wcsLineEdgeWarehouseLambdaUpdateWrapper.set(WcsLineEdgeWarehouse::getContainerCode,containerCode)
                .set(WcsLineEdgeWarehouse::getStatus,status)
                .eq(WcsLineEdgeWarehouse::getId,id);
        return wcsLineEdgeWarehouseMapper.update(null,wcsLineEdgeWarehouseLambdaUpdateWrapper) >0;
    }

    @Override
    public List<WcsLineEdgeWarehouse> selectAll() {
        return wcsLineEdgeWarehouseMapper.selectList(null);
    }

    @Override
    public List<WcsLineEdgeWarehouse> selectRegionContainerCodeisNotNull(String region) {
        LambdaQueryWrapper<WcsLineEdgeWarehouse> wcsLineEdgeWarehouseLambdaQueryWrapper = new LambdaQueryWrapper<>();
        wcsLineEdgeWarehouseLambdaQueryWrapper.eq(WcsLineEdgeWarehouse::getRegion,region)
                .eq(WcsLineEdgeWarehouse::getIsLock,0)
                .isNotNull(WcsLineEdgeWarehouse::getContainerCode);
        return wcsLineEdgeWarehouseMapper.selectList(wcsLineEdgeWarehouseLambdaQueryWrapper);
    }

    @Override
    public List<WcsLineEdgeWarehouse> selectRegion(String region) {
        LambdaQueryWrapper<WcsLineEdgeWarehouse> wcsLineEdgeWarehouseLambdaQueryWrapper = new LambdaQueryWrapper<>();
        wcsLineEdgeWarehouseLambdaQueryWrapper.eq(WcsLineEdgeWarehouse::getRegion,region)
                .eq(WcsLineEdgeWarehouse::getIsLock,0);
        return wcsLineEdgeWarehouseMapper.selectList(wcsLineEdgeWarehouseLambdaQueryWrapper);
    }

    @Override
    public boolean updateBuzzerStatus(String region, int buzzerStatus) {
        LambdaUpdateWrapper<WcsLineEdgeWarehouse> wcsLineEdgeWarehouseLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        wcsLineEdgeWarehouseLambdaUpdateWrapper.set(WcsLineEdgeWarehouse::getBuzzerStatus,buzzerStatus)
                .eq(WcsLineEdgeWarehouse::getRegion,region);
        return wcsLineEdgeWarehouseMapper.update(null,wcsLineEdgeWarehouseLambdaUpdateWrapper)>0;
    }

    @Override
    public List<WcsLineEdgeWarehouse> floorQueryAvailableStorage(String floor) {
        LambdaQueryWrapper<WcsLineEdgeWarehouse> wcsLineEdgeWarehouseLambdaQueryWrapper = new LambdaQueryWrapper<>();
        wcsLineEdgeWarehouseLambdaQueryWrapper.eq(WcsLineEdgeWarehouse::getFloor,floor)
                .eq(WcsLineEdgeWarehouse::getStatus,0)
                .eq(WcsLineEdgeWarehouse::getIsLock,0)
                .eq(WcsLineEdgeWarehouse::getCcdStatus,0);
        return wcsLineEdgeWarehouseMapper.selectList(wcsLineEdgeWarehouseLambdaQueryWrapper);
    }

    @Override
    public List<WcsLineEdgeWarehouse> selectRegionAndStatus(String region, int status) {
        LambdaQueryWrapper<WcsLineEdgeWarehouse> wcsLineEdgeWarehouseLambdaQueryWrapper = new LambdaQueryWrapper<>();
        wcsLineEdgeWarehouseLambdaQueryWrapper.eq(WcsLineEdgeWarehouse::getRegion,region)
                .eq(WcsLineEdgeWarehouse::getStatus,status)
                .eq(WcsLineEdgeWarehouse::getCcdStatus,0)
                .eq(WcsLineEdgeWarehouse::getIsLock,0);
        return wcsLineEdgeWarehouseMapper.selectList(wcsLineEdgeWarehouseLambdaQueryWrapper);
    }

    @Override
    public List<WcsLineEdgeWarehouse> selectRegionAvailableSize(String region) {
        LambdaQueryWrapper<WcsLineEdgeWarehouse> wcsLineEdgeWarehouseLambdaQueryWrapper = new LambdaQueryWrapper<>();
        wcsLineEdgeWarehouseLambdaQueryWrapper.eq(WcsLineEdgeWarehouse::getRegion,region)
                .notIn(WcsLineEdgeWarehouse::getStatus,0,1);
        return wcsLineEdgeWarehouseMapper.selectList(wcsLineEdgeWarehouseLambdaQueryWrapper);
    }

    @Override
    public List<WcsLineEdgeWarehouse> selectRegionInStatus(String region, Integer status, Integer status1) {
        LambdaQueryWrapper<WcsLineEdgeWarehouse> wcsLineEdgeWarehouseLambdaQueryWrapper = new LambdaQueryWrapper<>();
        wcsLineEdgeWarehouseLambdaQueryWrapper.eq(WcsLineEdgeWarehouse::getRegion,region)
                .in(WcsLineEdgeWarehouse::getStatus,status,status1)
                .eq(WcsLineEdgeWarehouse::getIsLock,0);
        return wcsLineEdgeWarehouseMapper.selectList(wcsLineEdgeWarehouseLambdaQueryWrapper);
    }

    @Override
    public Integer anomalyAreaGtThreeSize(String region) {
        LambdaQueryWrapper<WcsLineEdgeWarehouse> wcsLineEdgeWarehouseLambdaQueryWrapper = new LambdaQueryWrapper<>();
        wcsLineEdgeWarehouseLambdaQueryWrapper.eq(WcsLineEdgeWarehouse::getRegion,region)
                .eq(WcsLineEdgeWarehouse::getIsLock,0)
                .gt(WcsLineEdgeWarehouse::getStatus,1);
        return Math.toIntExact(wcsLineEdgeWarehouseMapper.selectCount(wcsLineEdgeWarehouseLambdaQueryWrapper));
    }

    @Override
    public List<WcsLineEdgeWarehouse> selectFloor(String floor) {
        LambdaQueryWrapper<WcsLineEdgeWarehouse> wcsLineEdgeWarehouseLambdaQueryWrapper = new LambdaQueryWrapper<>();
        wcsLineEdgeWarehouseLambdaQueryWrapper.like(WcsLineEdgeWarehouse::getFloor,floor);
        return wcsLineEdgeWarehouseMapper.selectList(wcsLineEdgeWarehouseLambdaQueryWrapper);
    }

    @Override
    public boolean updateEmptyTrayQuantityStatus(String storageCode, Integer quantity, Integer status) {
        LambdaUpdateWrapper<WcsLineEdgeWarehouse> wcsLineEdgeWarehouseLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        wcsLineEdgeWarehouseLambdaUpdateWrapper.set(WcsLineEdgeWarehouse::getEmptyTrayQuantity,quantity)
                .set(WcsLineEdgeWarehouse::getStatus,status)
                .eq(WcsLineEdgeWarehouse::getStorageCode,storageCode);
        return wcsLineEdgeWarehouseMapper.update(null,wcsLineEdgeWarehouseLambdaUpdateWrapper) >0;
    }

    @Override
    public WcsLineEdgeWarehouse selectStorageCodeDoesItExist(String storageCode) {
        LambdaQueryWrapper<WcsLineEdgeWarehouse> wcsLineEdgeWarehouseLambdaQueryWrapper = new LambdaQueryWrapper<>();
        wcsLineEdgeWarehouseLambdaQueryWrapper.eq(WcsLineEdgeWarehouse::getStorageCode,storageCode);
        return wcsLineEdgeWarehouseMapper.selectOne(wcsLineEdgeWarehouseLambdaQueryWrapper);
    }

    @Override
    public boolean updateIslocck(String storageCode, Integer isLock) {
        LambdaUpdateWrapper<WcsLineEdgeWarehouse> wcsLineEdgeWarehouseLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        wcsLineEdgeWarehouseLambdaUpdateWrapper.set(WcsLineEdgeWarehouse::getIsLock,isLock)
                .eq(WcsLineEdgeWarehouse::getStorageCode,storageCode);
        return wcsLineEdgeWarehouseMapper.update(null,wcsLineEdgeWarehouseLambdaUpdateWrapper) >0;
    }

    @Override
    public List<WcsLineEdgeWarehouse> regionQueryAvailableStorage(String region) {
        LambdaQueryWrapper<WcsLineEdgeWarehouse> wcsLineEdgeWarehouseLambdaQueryWrapper = new LambdaQueryWrapper<>();
        wcsLineEdgeWarehouseLambdaQueryWrapper.eq(WcsLineEdgeWarehouse::getRegion,region)
                .eq(WcsLineEdgeWarehouse::getStatus,0)
                .eq(WcsLineEdgeWarehouse::getIsLock,0)
                .eq(WcsLineEdgeWarehouse::getCcdStatus,0);
        return wcsLineEdgeWarehouseMapper.selectList(wcsLineEdgeWarehouseLambdaQueryWrapper);
    }

    @Override
    public List<WcsLineEdgeWarehouse> queryAvailableFloor(String floor) {
        LambdaQueryWrapper<WcsLineEdgeWarehouse> wcsLineEdgeWarehouseLambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (floor.isEmpty()){
            wcsLineEdgeWarehouseLambdaQueryWrapper.eq(WcsLineEdgeWarehouse::getStatus,0)
                    .eq(WcsLineEdgeWarehouse::getIsLock,0)
                    .eq(WcsLineEdgeWarehouse::getCcdStatus,0);
        }else {
            wcsLineEdgeWarehouseLambdaQueryWrapper.eq(WcsLineEdgeWarehouse::getFloor,floor)
                    .eq(WcsLineEdgeWarehouse::getStatus,0)
                    .eq(WcsLineEdgeWarehouse::getIsLock,0)
                    .eq(WcsLineEdgeWarehouse::getCcdStatus,0);
        }
        return wcsLineEdgeWarehouseMapper.selectList(wcsLineEdgeWarehouseLambdaQueryWrapper);
    }

    @Override
    public List<WcsLineEdgeWarehouse> queryRegion(String floor) {
        LambdaQueryWrapper<WcsLineEdgeWarehouse> wcsLineEdgeWarehouseLambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (floor.isEmpty()){
            return wcsLineEdgeWarehouseMapper.selectList(wcsLineEdgeWarehouseLambdaQueryWrapper);
        }
        wcsLineEdgeWarehouseLambdaQueryWrapper.eq(WcsLineEdgeWarehouse::getFloor,floor);
        return wcsLineEdgeWarehouseMapper.selectList(wcsLineEdgeWarehouseLambdaQueryWrapper);
    }

    @Override
    public List<WcsLineEdgeWarehouse> deliveryQuereStorageCode(String floor, String region,String destination) {
        LambdaQueryWrapper<WcsLineEdgeWarehouse> wcsLineEdgeWarehouseLambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (!floor.isEmpty()){
            wcsLineEdgeWarehouseLambdaQueryWrapper.eq(WcsLineEdgeWarehouse::getFloor,floor);
        }
        if (!region.isEmpty()){
            wcsLineEdgeWarehouseLambdaQueryWrapper.eq(WcsLineEdgeWarehouse::getRegion,region);
        }
        if (!destination.isEmpty()){
            wcsLineEdgeWarehouseLambdaQueryWrapper.eq(WcsLineEdgeWarehouse::getStorageCode,destination);
        }
        wcsLineEdgeWarehouseLambdaQueryWrapper.eq(WcsLineEdgeWarehouse::getStatus,0)
                .eq(WcsLineEdgeWarehouse::getIsLock,0)
                .eq(WcsLineEdgeWarehouse::getCcdStatus,0);
        return wcsLineEdgeWarehouseMapper.selectList(wcsLineEdgeWarehouseLambdaQueryWrapper);
    }

    @Override
    public WcsLineEdgeWarehouse selectLtStatus(String storageCode, Integer status) {
        LambdaQueryWrapper<WcsLineEdgeWarehouse> wcsLineEdgeWarehouseLambdaQueryWrapper = new LambdaQueryWrapper<>();
        wcsLineEdgeWarehouseLambdaQueryWrapper.eq(WcsLineEdgeWarehouse::getStorageCode,storageCode)
                .eq(WcsLineEdgeWarehouse::getIsLock,0)
                .ge(WcsLineEdgeWarehouse::getStatus,status);
        return wcsLineEdgeWarehouseMapper.selectOne(wcsLineEdgeWarehouseLambdaQueryWrapper);
    }

    @Override
    public List<WcsLineEdgeWarehouse> selectRegionContainerCodeIsNull(String region) {
        LambdaQueryWrapper<WcsLineEdgeWarehouse> wcsLineEdgeWarehouseLambdaQueryWrapper = new LambdaQueryWrapper<>();
        wcsLineEdgeWarehouseLambdaQueryWrapper.eq(WcsLineEdgeWarehouse::getRegion,region)
                .eq(WcsLineEdgeWarehouse::getIsLock,0)
                .isNull(WcsLineEdgeWarehouse::getContainerCode);
        return wcsLineEdgeWarehouseMapper.selectList(wcsLineEdgeWarehouseLambdaQueryWrapper);
    }
}
